package com.example.chatchatbackend.service.impl.sendJob;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chatchatbackend.entity.dto.send.SendNotice;
import com.example.chatchatbackend.entity.vo.response.SendNoticeVO;
import com.example.chatchatbackend.mapper.SendNoticeMapper;
import com.example.chatchatbackend.service.sendJob.SendNoticeService;
import com.example.chatchatbackend.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Service
public class SendNoticeServiceImpl extends ServiceImpl<SendNoticeMapper, SendNotice> implements SendNoticeService {

    private static final LocalDateTime currentTime  = TimeUtil.Time_Now.getCurrentTime();

    @Autowired
    private SendNoticeMapper sendNoticeMapper;

    @Override
    public List<SendNoticeVO> getAllSendNotice() {
        List<SendNotice> sendNoticeList = this.query().list();
        List<SendNoticeVO> sendNoticeVOList = new ArrayList<>();
        for(SendNotice sendNotice : sendNoticeList){
            sendNoticeVOList.add(new SendNoticeVO(sendNotice.getId(),sendNotice.getTitle(),sendNotice.getContent(),sendNotice.getCreatedTime(),sendNotice.getSendTime(),sendNotice.getUpdateTime(),sendNotice.getEndTime(),sendNotice.getCreatorId(),sendNotice.getStatus(),sendNotice.getIsSend()));
        }
        return sendNoticeVOList;
    }

    @Override
    public SendNoticeVO insertSendNotice(SendNoticeVO sendNoticeVO) {
        SendNotice comment = new SendNotice(null,sendNoticeVO.getTitle(),sendNoticeVO.getContent(),this.currentTime,sendNoticeVO.getSendTime(),this.currentTime,sendNoticeVO.getEndTime(),sendNoticeVO.getCreatorId(),sendNoticeVO.getStatus(),0);
        if(this.save(comment))
            return sendNoticeVO;
        return null;
    }

    @Override
    public List<SendNoticeVO> insertSendNoticeList(List<SendNoticeVO> sendNoticeVOList) {

        List<SendNotice> sendNoticeList = new ArrayList<>();

        for (SendNoticeVO sendNoticeVO : sendNoticeVOList) {
            sendNoticeList.add(new SendNotice(null,sendNoticeVO.getTitle(),sendNoticeVO.getContent(),this.currentTime,sendNoticeVO.getSendTime(),this.currentTime,sendNoticeVO.getEndTime(),sendNoticeVO.getCreatorId(),sendNoticeVO.getStatus(),0));
        }
        sendNoticeMapper.insertList(sendNoticeList);
        return sendNoticeVOList;
    }


    @Override
    public IPage<SendNotice> pageThree(Integer pageNum, Integer pageSize) {
        Page<SendNotice> page = new Page<>(pageNum, pageSize);
        IPage<SendNotice> sendNoticeIPage = sendNoticeMapper.selectPage(page, null);
        return sendNoticeIPage;
    }


    @Override
    public SendNoticeVO updateSendNotice(SendNoticeVO sendNoticeVO) {
        SendNotice sendNotice = new SendNotice();
        BeanUtils.copyProperties(sendNoticeVO, sendNotice);
        sendNotice.setUpdateTime(this.currentTime);
        sendNoticeMapper.updateSendNotice(sendNotice);
        return sendNoticeVO;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        sendNoticeMapper.deleteBatchIds(ids);
    }

    public List<SendNoticeVO> findByTitle(String title) {

        LambdaQueryWrapper<SendNotice> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(StringUtils.hasText(title), SendNotice::getContent, title);

        List<SendNotice> sendNoticeList = sendNoticeMapper.selectList(wrapper);
        List<SendNoticeVO> sendNoticeVOList = new ArrayList<>();

        for (SendNotice sendNotice : sendNoticeList) {
            SendNoticeVO sendNoticeVO = new SendNoticeVO();
            BeanUtils.copyProperties(sendNotice, sendNoticeVO);
            sendNoticeVOList.add(sendNoticeVO);  // 添加到结果列表
        }
        return sendNoticeVOList;
    }

    @Override
    public List<SendNoticeVO> findTasksToSend(LocalDateTime currentTime) {


        List<SendNotice> sendNotices = sendNoticeMapper.findSendNoticeList("0",currentTime);

        List<SendNoticeVO> sendStudentVos = sendNotices.stream()
                .map(sendNotice -> {
                    SendNoticeVO sendNoticeVO = new SendNoticeVO();
                    BeanUtils.copyProperties(sendNotice, sendNoticeVO);
                    return sendNoticeVO;
                })
                .collect(Collectors.toList()); // 使用 collect(Collectors.toList())

        return sendStudentVos;
    }
}
